#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main() {
    int pipefd[2];
    pid_t cpid;
    char buffer[100];
    const char *data = "20221302李锦程";

    if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    cpid = fork();
    if (cpid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (cpid == 0) {    /* 子进程 */
        close(pipefd[1]);          /* 关闭写端 */
        read(pipefd[0], buffer, sizeof(buffer)); /* 读取数据 */
        printf("子进程读取到的数据: %s\n", buffer);
        close(pipefd[0]);          /* 关闭读端 */
        _exit(EXIT_SUCCESS);
    } else {            /* 父进程 */
        close(pipefd[0]);          /* 关闭读端 */
        write(pipefd[1], data, strlen(data)); /* 写入数据 */
        close(pipefd[1]);          /* 关闭写端 */
        wait(NULL);                /* 等待子进程结束 */
        exit(EXIT_SUCCESS);
    }
}

